<?

require_once  $_SERVER['DOCUMENT_ROOT']."/../_lib/base/common.php";
require_once SITE_ROOT_PATH."/_lib/classes/class.destinations.php";
require_once SITE_ROOT_PATH."/_lib/classes/class.hotel_instances_mcs.php";
require_once SITE_ROOT_PATH."/_lib/classes/class.hotel_instances_you.php";
require_once SITE_ROOT_PATH."/_lib/classes/class.upload.php";
require_once SITE_ROOT_PATH."/_lib/classes/class.template.php";
require_once SITE_ROOT_PATH."/_lib/classes/class.hotels.php";
require_once SITE_ROOT_PATH."/_lib/classes/class.photos.php";
require_once SITE_ROOT_PATH."/_lib/classes/class.tree.php";  
require_once SITE_ROOT_PATH."/_lib/classes/class.reviews.php"; 
require_once SITE_ROOT_PATH."/_lib/classes/class.user_auth.php";



	// See if user is logged in 
	$user_auth = new user_auth();
	$user_auth->user_auth_cp();
	$template = new template();





// FUNCTION // Display hotels
function display_home(){

	$tree = new tree();
	$photos = new photos();	
	$hotels = new hotels();
	
if(!empty($_POST)){
	 $_SESSION['hotel_search_params'] = $_POST;
}


?>


<h2>Hotels</h2>


<?
if($GLOBALS['note']){
	echo display_message($GLOBALS['note']);
}
?>

<p>To add a new hotel click <a href="<?= $_SERVER['PHP_SELF'] ?>?action=step_1_insert">here</a></p>





<h3>Search Hotels</h3>

<div class="border_box">



<fieldset><legend>Search Hotels</legend></fieldset>


<form action="<?= $_SERVER['PHP_SELF'] ?>" method="post" >

<input type="hidden" name="search" value="true" />

<table class="libra">
<tr>
<td width="150">Prefix</td>
<td>
<select name="prefix">	
<option value=""></option>
<option value="a" <?= selected("a",$_SESSION['hotel_search_params']['prefix']) ?>>a</option>
<option value="b" <?= selected("b",$_SESSION['hotel_search_params']['prefix']) ?>>b</option>
<option value="c" <?= selected("c",$_SESSION['hotel_search_params']['prefix']) ?>>c</option>
<option value="d" <?= selected("d",$_SESSION['hotel_search_params']['prefix']) ?>>d</option>
<option value="e" <?= selected("e",$_SESSION['hotel_search_params']['prefix']) ?>>e</option>
<option value="f" <?= selected("f",$_SESSION['hotel_search_params']['prefix']) ?>>f</option>
<option value="g" <?= selected("g",$_SESSION['hotel_search_params']['prefix']) ?>>g</option>
<option value="h" <?= selected("h",$_SESSION['hotel_search_params']['prefix']) ?>>h</option>
<option value="i" <?= selected("i",$_SESSION['hotel_search_params']['prefix']) ?>>i</option>
<option value="j" <?= selected("j",$_SESSION['hotel_search_params']['prefix']) ?>>j</option>
<option value="k" <?= selected("k",$_SESSION['hotel_search_params']['prefix']) ?>>k</option>
<option value="l" <?= selected("l",$_SESSION['hotel_search_params']['prefix']) ?>>l</option>
<option value="m" <?= selected("m",$_SESSION['hotel_search_params']['prefix']) ?>>m</option>
<option value="n" <?= selected("n",$_SESSION['hotel_search_params']['prefix']) ?>>n</option>    
<option value="o" <?= selected("o",$_SESSION['hotel_search_params']['prefix']) ?>>o</option>
<option value="p" <?= selected("p",$_SESSION['hotel_search_params']['prefix']) ?>>p</option>
<option value="q" <?= selected("q",$_SESSION['hotel_search_params']['prefix']) ?>>q</option>
<option value="r" <?= selected("r",$_SESSION['hotel_search_params']['prefix']) ?>>r</option>    
<option value="s" <?= selected("s",$_SESSION['hotel_search_params']['prefix']) ?>>s</option>
<option value="t" <?= selected("t",$_SESSION['hotel_search_params']['prefix']) ?>>t</option>
<option value="u" <?= selected("u",$_SESSION['hotel_search_params']['prefix']) ?>>u</option>
<option value="v" <?= selected("v",$_SESSION['hotel_search_params']['prefix']) ?>>v</option>
<option value="w" <?= selected("w",$_SESSION['hotel_search_params']['prefix']) ?>>w</option>
<option value="x" <?= selected("x",$_SESSION['hotel_search_params']['prefix']) ?>>x</option>
<option value="y" <?= selected("y",$_SESSION['hotel_search_params']['prefix']) ?>>y</option>
<option value="z" <?= selected("z",$_SESSION['hotel_search_params']['prefix']) ?>>z</option>
</select>
</td>
</tr>
<tr>
<td>Name</td>
<td><input type="text" name="name" value="<?= $_SESSION['hotel_search_params']['name'] ?>" /></td>
</tr>
<tr>
<td>Resort</td>
<td><select name="parent_id">

<option value=""></option>
<?


$params['type_id'] = '2';
$params['cache']['seconds'] = '600';
$resorts = $tree->get_children($params);

unset($params);
foreach($resorts as $resort){

	echo "<option value=\"".$resort['tree_id']."\" ".selected($resort['tree_id'],$_SESSION['hotel_search_params']['parent_id']).">".$resort['name']."</option>";
}


?>

</select>
</td>
</tr>
<tr>
<td>Text within description</td>
<td><input type="text" name="desc_text" value="<?= $_SESSION['hotel_search_params']['desc_text'] ?>" /></td>
</tr>
<tr>
<td>Sort By</td>
<td>
<select name="order">
<option value="" <?= selected("",$_SESSION['hotel_search_params']['order']) ?>>Name</option>
<option value="tree" <?= selected("tree",$_SESSION['hotel_search_params']['order']) ?>>Tree ID</option>
<option value="updated" <?= selected("updated",$_SESSION['hotel_search_params']['order']) ?>>Last Updated</option>
</select>
</td>
</tr>
<tr>
<td></td>
<td><input type="submit" name="submit" value="submit"/></td>
</tr>
</table>


</form>

</div>

<br />


<?


		//$_SESSION['hotel_search_params']['cache'] = true;
		//$_SESSION['hotel_search_params']['cache']['seconds'] = 600;
		$values = $hotels->search_hotels($_SESSION['hotel_search_params']);
		//unset($params);


		$hotel_instances_mcs = new hotel_instances_mcs();


if($values){

?>

<table class="aqua">
  <tr bgcolor="#CCCCCC">
    <th width="250">Name</th>
    <th width="80">Tree ID</th>
    <th width="150">Description</th>
    <th width="50">Photos</th>
    <th width="200">Resort</th>
    <th width="120">No. instances</th>
	<th width="120">Last updated</th>
    
</tr>

<? foreach($values as $value){ 


?>
  <tr>
    <td><a href="<?= $_SERVER['PHP_SELF']."?action=view&tree_id=".$value['tree_id'] ?>"><?= $value['name'] ?></a></td>
    <td><?= $value['tree_id'] ?></td>
    <td><?= substr($value['description'], 0, 20 ) ?></td>
    <td align="center"><?
    
	$cache['cache']['seconds'] = '600';
	$hotel_photos = $photos->get_photos($value['tree_id'], $cache);
	unset($cache);
	echo count($hotel_photos);
	
	?></td>
    <td><?
	
	
	if($value['parent_id']){
		$params['tree_id'] = $value['parent_id'];
		$params['cache']['seconds'] = '600';
		$resort_values = $tree->get_branch($params);
		$resort_values = $resort_values[0];
		unset($params);
		echo $resort_values['name']." (".$resort_values['tree_id'].")"; 
	}else{
		echo "na";
	}
	
	
	?></td>
    
     <td><?
	
	$params = array();
	$params['tree_id'] = $value['tree_id'];
	$params['cache']['seconds'] = '600';
	$num_hotel_instances_mcs = count($hotel_instances_mcs->search_hotel_instances($params));
	echo $num_instances;
	unset($params,$num_instances);
	
	 ?></td>
    <td><?= ($value['last_updated'] ? date('d/m/Y',$value['last_updated']) :  "Never updated"); ?></td>
    
    
    
	


</tr>
<? } ?>

</table>

<?
}

}















// FUNCTION // Display update form =========================================
function display_form($values=''){

    $hotels = new hotels();
	$tree = new tree();
	
	$notification = $GLOBALS['note'];
	
?>


<h2>Hotels</h2>






<h3>Details</h3>

<?
if($notification){
	echo display_message($notification);
}
?>

<div class="border_box">





<form method="post" action="<?= $_SERVER['PHP_SELF'] ?>" name="car_form"  >

    <input type="hidden" name="action" value="step_2">
    <? if($values['tree_id']){ ?>
	<input type="hidden" name="tree_id" value="<?= $values['tree_id'] ?>">
	<? } ?>
	


<fieldset><legend>General</legend></fieldset>


<table class="libra">
  <tr>
    <td width="200"><strong>Name</strong></td>
    <td ><input name="name" type="text" value="<?= $values['name'] ?>" size="40" ></td>
  </tr>

  <tr>
    <td><strong>Description</strong></td>
    <td><textarea name="description" cols="100" rows="20" class="my_textarea"><?= $values['description'] ?>
</textarea></td>
</tr>
<tr>
<td><strong>Rating</strong></td>
<td><select name="hotel_rating">
<option></option>
<option value="1" <?= selected($values['hotel_rating'],'1') ?>>1 Star</option>
<option value="2" <?= selected($values['hotel_rating'],'2') ?>>2 Star</option>
<option value="3" <?= selected($values['hotel_rating'],'3') ?>>3 Star</option>
<option value="4" <?= selected($values['hotel_rating'],'4') ?>>4 Star</option>
<option value="5" <?= selected($values['hotel_rating'],'5') ?>>5 Star</option>
</select>
</td>
</tr>


</table>




<br />


<fieldset><legend>Resort</legend></fieldset>


<table class="libra">
  <tr>
    <td width="200"><strong>Resort</strong></td>
    <td>
<select name="parent_id">

<option value=""></option>
<?


$params['type_id'] = '2';
$params['cache']['seconds'] = '600';
$resorts = $tree->get_children($params);
unset($params);
foreach($resorts as $resort){


$params['tree_id'] = $resort['parent_id'];
$params['cache']['seconds'] = '600';
$parent_destination = $tree->get_branch($params);
$parent_destination = $parent_destination[0];
unset($params);

	echo "<option value=\"".$resort['tree_id']."\" ".selected($resort['tree_id'],$values['parent_id']).">".$resort['name'].", ".$parent_destination['name']."</option>";
}


?>
	</select>
	 </td>
</tr>
</table>





<br />




      
<fieldset><legend>Longtitude & Latitude</legend></fieldset>

<table class="libra">
    
      <tr> 
        <td width="200"><strong>
          <label>Longtitude</label>
        </strong></td>
        <td><input name="longtitude" type="text" value="<?= $values['longtitude'] ?>" /></td>
      </tr>
      <tr> 
        <td><strong>
          <label>Latitude</label>
        </strong></td>
        <td><input name="latitude" type="text" value="<?= $values['latitude'] ?>" /></td>
      </tr>
    
      <tr> 
        <td><strong>
          <label>Google Maps</label>
        </strong></td>
        <td><input type="checkbox" name="show_google_map" value="1" <?= checked($values['show_google_map'], '1') ?> /></td>
      </tr>
    </table>
    
    
<br />


<fieldset><legend>Misc</legend></fieldset>


<table class="libra">
 <tr>
   <td width="200"><strong>Official website</strong></td>
   <td><input name="website_url" value="<?= $values['website_url'] ?>" size="40" ></td>
</tr>
  <tr>
    <td><strong>Notes</strong></td>
    <td><textarea name="notes" cols="100" rows="3" class="my_textarea"><?= $values['notes'] ?>
</textarea></td>
</tr>
</table>



  <center>
    <input type="button" value="Cancel" onClick="history.back();" class="form_button">
    <input type="submit" name="submit" value="Next" class="form_button" onclick='javascript:getresults();'></td>
    </center>

</form>


<?



}
// END FUNCTION ====================================================







// FUNCTION // Display photos form
function display_photos_form($form_errors=''){


?>

<h2>Hotels</h2>

<h3>Step 2</h3>



<div class="border_box">





<form action="<?= $_SERVER['PHP_SELF'] ?>" method="post" enctype="multipart/form-data"  >

<input type="hidden" name="action" value="upload_photo" />
<input type="hidden" name="form_submitted" value="true" />


<?
if(!empty($_SESSION['hotel']['photos'])){
	$uploaded_count = count($_SESSION['hotel']['photos']);
}else{
	$uploaded_count = 0;
}


if($uploaded_count < '10'){
?>

<fieldset><legend>Upload new photo</legend></fieldset>

<?
if($form_errors){
	$my_message = array();
	$my_message['type'] = 'neg';
	$my_message['message'] = "There was a problem with the information submitted";
	$my_message['list'] = $form_errors;
	echo display_message($my_message);
}
?>


<?

	if($uploaded_count){
		$i = $uploaded_count;
	}else{
		$i = 0;
	}


?>
<table class="libra">
<tr>
<td width="150"><label>Photo <?= $i+1 ?></label></td>
<td><input name="photo_<?= $i ?>" type="file" /></td>
</tr>
<tr>
<td></td>
<td><input type="submit" name="submit" value="submit" /></td>
</tr>
</table>

</form>

<? } ?>




<?
if(!empty($uploaded_count)){
?>


<fieldset><legend>Current photos</legend></fieldset>



<table class="libra">
<?
for($j=0;$j<$uploaded_count;$j++){
?>

<tr>
<td width="150">Photo <?= $j+1 ?></td>
<td width="120"><img src="<?= UPLOAD_HTTP_TEMP.$_SESSION['hotel']['photos'][$j]['photo_url_thumb'] ?>" /></td>
<td>

<? if($j==0) { ?>
<h5>Main photo</h5>
<ul class="qlinks"><li><a href="<?= $_SERVER['PHP_SELF'] ?>?action=delete_photo&photo_no=<?= $j ?>" onclick="return confirm('Do you really want to delete this photo?')">Delete this photo</a></li></ul>
<? }else{ ?>
<ul class="qlinks">
<li><a href="<?= $_SERVER['PHP_SELF'] ?>?action=make_main&photo_no=<?= $j ?>">Make this the main photo</a></li>
<li><a href="<?= $_SERVER['PHP_SELF'] ?>?action=delete_photo&photo_no=<?= $j ?>" onclick="return confirm('Do you really want to delete this photo?')">Delete this photo</a></li>
</ul>
<? } ?>


</td>
</tr>

<? } ?>
</table>
<? } ?>




<form action="<?= $_SERVER['PHP_SELF'] ?>" method="get">

    <input type="hidden" name="action" value="step_3"/>
 
    <input type="button" value="Back" onclick="history.back();" />
    <input type="submit" name="submit" value="Next" />

</form>




<?



}
// END FUNCTION








    
// FUNCTION // Preview advert
function preview_advert($values='' ){

$hotels = new hotels();
$tree = new tree();
$photos = new photos();

?>

<h2>Hotels</h2>

<h3>Preview</h3>

<div class="border_box">


<fieldset><legend>General</legend></fieldset>


<table class="libra">
  <tr>
    <td width="200"><strong>Name</strong></td>
    <td ><?= $values['name'] ?></td>
  </tr>

  <tr>
    <td><strong>Description</strong></td>
    <td><?= nl2br($values['description']); ?></td>
</tr>


<tr>
<td><strong>Rating</strong></td>
<td><?= $values['hotel_rating']; ?></td>
</tr>

</table>

<br />



<fieldset><legend>Resort</legend></fieldset>


<table class="libra">
  <tr>
    <td width="200"><strong>Resort</strong></td>
    <td><?
	

	$params['tree_id'] = $values['parent_id'];
	$resort_values = $tree->get_branch($params);
	
	$resort_values = $resort_values[0];
	unset($params);
	echo $resort_values['name']." (".$values['parent_id'].")";
	
	?></td>
</tr>
</table>

<br />



<fieldset><legend>Photos</legend></fieldset>


<table class="libra">
  <tr>
    <td width="200"><strong>Photos</strong></td>
    <td><?

    if($values['tree_id']) $current_photos = $photos->get_photos($values['tree_id']);

    if(!$_SESSION['hotel']['photos'] AND !$current_photos){
		echo "<img src='".UPLOAD_HTTP_PERM."na.gif'>";
	}else{
		
	
		for($i = 0; $i < count($_SESSION['hotel']['photos']); $i++){
			
			if($_SESSION['hotel']['photos'][$i]){
			
				echo "<img src='".UPLOAD_HTTP_TEMP.$_SESSION['hotel']['photos'][$i]['photo_url_thumb']."' class='thumbnail' name='thumb".$value['photo_id']."' />";
			
			}elseif($current_photos[$i]){
			
				
				echo "<img src='".UPLOAD_HTTP_PER.$current_photos[$i]['photo_url_thumb']."' class='thumbnail' name='thumb".$value['photo_id']."' />";
				
			}
		
		}
        
    }
    
?></td>
</tr>
</table>

<br />





<fieldset><legend>Longtitude & Latitude</legend></fieldset>


<table class="libra">
<tr>
    <td width="200"><strong>Longtitude</strong></td>
    <td><?= $values['longtitude']; ?></td>
</tr>
<tr>
    <td><strong>Latitude</strong></td>
    <td><?= $values['latitude']; ?></td>
</tr>  
</table>
<br />





<fieldset><legend>Misc</legend></fieldset>


<table class="libra">
  <tr>
    <td width="200"><strong>Official website</strong></td>
    <td><?= $values['website_url']; ?></td>
</tr>
<tr>
  <td><strong>Notes</strong></td>
  <td><?= nl2br($values['notes']); ?></td>
</tr>
</table>
<br />




<fieldset><legend>Instances</legend></fieldset>



<table class="libra">
<tr>
    <td width="200"><strong>Micros Linked Instances</strong></td>
    <td>
	
	
<table  class="libra">
<tr bgcolor="#CCCCCC">
<td width="90">Instance ID</td>
<td width="150">Hotel name</td>
<td width="100">Tour Op</td>
<td width="100">Hotel Code</td>
<td width="100">Airport</td>
<td width="100">Date linked</td>

</tr>	

	<?
	
	$params['tree_id'] = $values['tree_id'];
	$hotel_instances_mcs = new hotel_instances_mcs();
	$instances = $hotel_instances_mcs->search_hotel_instances($params);
	unset($params);
	
	
if($instances){
	foreach($instances as $instance_values){
	
?>
<tr>
<td><?= $instance_values['instance_id'] ?></td>
<td><?= $instance_values['hotel_name'] ?></td>
<td><?= $instance_values['tour_op'] ?></td>
<td><?= $instance_values['holiday_code'] ?></td>
<td><?= $instance_values['destination_airport'] ?></td>
<td><?= date('d/m/Y', $instance_values['date_linked']) ?></td>
</tr>
<?
	}
	unset($instances);
}
?>

</table>


</td>
</tr>

</table>






<table class="libra">
<tr>
    <td width="200"><strong>YouTravel Linked Instances</strong></td>
    <td>
	
	
<table  class="libra">
<tr bgcolor="#CCCCCC">
<td width="90">Instance ID</td>
<td width="150">Hotel name</td>
<td width="150">hotel_id_youtravel</td>

<td width="150">Date linked</td>
</tr>	

	<?
	
	$params['tree_id'] = $values['tree_id'];
	$hotel_instances_you = new hotel_instances_you();
	$instances = $hotel_instances_you->search_hotel_instances($params);
	unset($params);
	
if(!empty($instances)){
	foreach($instances as $instance_values){
	
?>
<tr>
<td><?= $instance_values['instance_id'] ?></td>
<td><?= $instance_values['hotel_name'] ?></td>
<td><?= $instance_values['hotel_id_youtravel'] ?></td>
<td><?= date('d/m/Y', $instance_values['date_linked']) ?></td>
</tr>
<?
	}
	unset($instances);
}
?>

</table>


</td>
</tr>

</table>



</div>

<br />


<form action="<?= $_SERVER['PHP_SELF'] ?>">

    <input type="hidden" name="action" value="step_4">

	<center>
    <input type="button" value="Back" onclick="history.back();" class="form_button">
	<input type="submit" value="Save" class="form_button" />
	</center>

</form>



<?

    
}
// END FUNCTION


    
// FUNCTION // View the hotel details
function view_details($values='' ){

$hotels = new hotels();
$destinations = new destinations();
$photos = new photos();
?>

<h2>Hotels</h2>

<h3>View</h3>
<?
if($GLOBALS['note']){
	echo display_message($GLOBALS['note']);
}
?>

<ul>
<li><a href="hotels.php?action=step_1_edit&tree_id=<?= $values['tree_id'] ?>">Edit details</a></li>
<li><a href="hotels.php?action=confirm_delete&tree_id=<?= $values['tree_id'] ?>" onclick="return confirm('Do you really want to delete this hotel?')"  >Delete this hotel</a></li>
</ul>



<div class="border_box">


<fieldset><legend>General</legend></fieldset>


<table class="libra">
  <tr>
    <td width="200"><strong>Name</strong></td>
    <td ><?= $values['name'] ?></td>
  </tr>
  <tr>
    <td><strong>Tree ID</strong></td>
    <td ><?= $values['tree_id'] ?></td>
  </tr>
  <tr>
    <td><strong>Description</strong></td>
    <td><?= nl2br($values['description']); ?></td>
</tr>
<tr>
<td><strong>Rating</strong></td>
<td><?= $values['hotel_rating']; ?></td>
</tr>
</table>

<br />



<fieldset><legend>Resort</legend></fieldset>


<table class="libra">
  <tr>
    <td width="200"><strong>Resort</strong></td>
    <td><?
	

	
	$tree = new tree();	
	$params['tree_id'] = $values['parent_id'];
	$resort_values = $tree->get_branch($params);
	echo $resort_values[0]['name'];
	unset($params);
	
	
	?></td>
</tr>
</table>

<br />



<fieldset><legend>Photos</legend></fieldset>


<table class="libra">
  <tr>
    <td width="200"><strong>Photos</strong></td>
    <td><?

    if($values['tree_id']){
		$current_photos = $photos->get_photos($values['tree_id']);
	}
	
    if(!$current_photos){
		echo "<img src='".UPLOAD_HTTP_PERM."na.gif'>";
	}else{
		
		foreach($current_photos as $current_photo){
		
			echo "<img src='".UPLOAD_HTTP_PERM.$current_photo['photo_url_thumb']."' class='thumbnail' name='thumb".$value['photo_id']."' />";
		
		}
        
    }
    
?></td>
</tr>
</table>

<br />







<fieldset><legend>Longtitude & Latitude</legend></fieldset>


<table class="libra">
<tr>
    <td width="200"><strong>Longtitude</strong></td>
    <td><?= $values['longtitude']; ?></td>
</tr>
<tr>
    <td><strong>Latitude</strong></td>
    <td><?= $values['latitude']; ?></td>
</tr>  
</table>
<br />



<fieldset><legend>Misc</legend></fieldset>


<table class="libra">
  <tr>
    <td width="200"><strong>Official website</strong></td>
    <td><?= $values['website_url']; ?></td>
</tr>
<tr>
  <td><strong>Last Updated</strong></td>
  <td><?= ($values['last_updated'] ? date('d/m/Y',$values['last_updated']) :  "Never updated"); ?></td>
</tr>
<tr>
    <td><strong>Notes</strong></td>
    <td><?= nl2br($values['notes']); ?></td>
</tr>
</table>
<br />


<fieldset><legend>Reviews</legend></fieldset>


<table class="libra">
  <tr>
    <td width="200"><strong>No. of reviews</strong></td>
    <td><?
	
	
	$reviews = new reviews();
	$params['tree_id'] = $values['tree_id'];
	$current_reviews = $reviews->get_reviews($params);
$current_reviews = $current_reviews[0];
	if(is_array($current_reviews))	echo count($current_reviews)+0;


	?></td>
</tr>
</table>
<br />



<fieldset><legend>Instances</legend></fieldset>


<table class="libra">
<tr>
    <td width="200"><strong>Micros Linked Instances</strong></td>
    <td>
	
	
<table  class="libra">
<tr bgcolor="#CCCCCC">
<td width="90">Instance ID</td>
<td width="150">Hotel name</td>
<td width="100">Tour Op</td>
<td width="100">Hotel Code</td>
<td width="100">Airport</td>
<td width="100">Date linked</td>
<td width="90">Actions</td>
</tr>	

	<?
	
	$params['tree_id'] = $values['tree_id'];
	$hotel_instances_mcs = new hotel_instances_mcs();
	$instances = $hotel_instances_mcs->search_hotel_instances($params);
	unset($params);
	
	
if($instances){
	foreach($instances as $instance_values){
	
?>
<tr>
<td><?= $instance_values['instance_id'] ?></td>
<td><?= $instance_values['hotel_name'] ?></td>
<td><?= $instance_values['tour_op'] ?></td>
<td><?= $instance_values['holiday_code'] ?></td>
<td><?= $instance_values['destination_airport'] ?></td>
<td><?= date('d/m/Y', $instance_values['date_linked']) ?></td>
<td><a onclick="return confirm('Do you really want to delete this instance?')" href="hotels_flagged_mcs.php?action=delete_linked&instance_id=<?= $instance_values['instance_id'] ?>&tree_id=<?= $values['tree_id'] ?>">Delete</a></td>
</tr>
<?
	}
	unset($instances);
}
?>

</table>


</td>
</tr>

</table>






<table class="libra">
<tr>
    <td width="200"><strong>YouTravel Linked Instances</strong></td>
    <td>
	
	
<table  class="libra">
<tr bgcolor="#CCCCCC">
<td width="90">Instance ID</td>
<td width="150">Hotel name</td>
<td width="150">hotel_id_youtravel</td>

<td width="150">Date linked</td>
<td width="90">Actions</td>
</tr>	

	<?
	
	$params['tree_id'] = $values['tree_id'];
	$hotel_instances_you = new hotel_instances_you();
	$instances = $hotel_instances_you->search_hotel_instances($params);
	unset($params);
	
if($instances){
	foreach($instances as $instance_values){
	
?>
<tr>
<td><?= $instance_values['instance_id'] ?></td>
<td><?= $instance_values['hotel_name'] ?></td>
<td><?= $instance_values['hotel_id_youtravel'] ?></td>
<td><?= date('d/m/Y', $instance_values['date_linked']) ?></td>
<td><a onclick="return confirm('Do you really want to delete this instance?')" href="hotels_flagged_you.php?action=delete&instance_id=<?=$instance_values['instance_id'] ?>">Delete</a></td>
</tr>
<?
	}
	unset($instances);
}
?>

</table>


</td>
</tr>

</table>



</div>

<br />




<?

    
}





// FUNCTION // confirm delete options 
function view_confirm_delete(){

$hotels = new hotels();
$destinations = new destinations();
$photos = new photos();



			$params['tree_id'] = $_GET['tree_id'];
			$values = $hotels->get_hotel($params);

?>

<h2>Hotels</h2>

<h3>Confirm Delete</h3>


<div class="border_box">

<h4>Do you really want to delete the following hotel?</h4>

<table class="libra">
  <tr>
    <td width="200"><strong>Tree ID</strong></td>
    <td ><?= $values['tree_id'] ?></td>
  </tr>
  <tr>
    <td width="200"><strong>Name</strong></td>
    <td ><?= $values['name'] ?></td>
  </tr>
</table>

<br />

<h4>Yes and do the following:</h4>

<form action="<?= $_SERVER['PHP_SELF'] ?>">

    <input type="hidden" value="<?= $values['tree_id'] ?>" name="delete_tree_id" />
    <input type="hidden" value="delete_move" name="action" />
    
<script>
jQuery(function ($) {

	
	function countChecked() {
		var opts = $("input:checked").length;
		var tree_id = $("#move_to_tree_id").value;
		// &(tree_id==false)
		if((n==false)){
			alert('test');
		}
	}
    $("#submiter").click(countChecked);



});


</script>
    
<table class="libra">
  <tr>
    <td width="200"><strong>Copy Data</strong></td>
    <td><input type="checkbox" value="true" name="copy_description" /> <label>Copy Description</label></td>
  </tr>
  <tr>
    <td></td>
    <td><input type="checkbox" value="true" name="copy_photos" /> <label>Copy Photos</label></td>
  </tr>
  <tr>
    <td></td>
    <td><input type="checkbox" value="true" name="copy_reviews" /> <label>Copy Reviews</label></td>
  </tr>
  <tr>
    <td></td>
    <td><input type="checkbox" value="true" name="copy_mcs_instances" /> <label>Copy Micros Instances</label></td>
  </tr>
  <tr>
    <td><strong>Tree ID</strong></td>
    <td ><input type="text" name="move_to_tree_id" id="move_to_tree_id"  /></td>
  </tr>
  <tr>
    <td></td>
    <td><input type="submit" id="submiter" value="Move & Delete" /></td>
  </tr>
</table>
</form>
<?
}





switch ($_REQUEST['action']) {
    default; 
		$template->head_control();
		display_home();
		$template->foot_control();
    break;
	case "step_1_insert";
        // clear any old advert session
		unset($_SESSION['hotel'],$_SESSION['branch']);
		$_SESSION['hotel']['type'] = "new";
		$template->head_control();
		display_form();
		$template->foot_control();
    break;
	case "step_1_edit";
		// clear any old advert session
        unset($_SESSION['hotel']); 
		if(isset($_GET['tree_id'])){
			
			$tree = new tree();
			$params['tree_id'] = $_GET['tree_id'];
			$hotel_values = $tree->get_branch($params,3);
			
			$_SESSION['hotel'] = $hotel_values[0];
			$_SESSION['hotel']['type'] = "edit";
			
			// Photos
			$photos = new photos();
			$current_photos = $photos->get_photos($hotel_values[0]['tree_id']);
			if(!empty($current_photos)){
				foreach($current_photos as $photo_values){
					// copy the exsiting images to the temp diretory
					@copy(UPLOAD_ABS_PERM.$photo_values['photo_url_full'], UPLOAD_ABS_TEMP.$photo_values['photo_url_full']);
					@copy(UPLOAD_ABS_PERM.$photo_values['photo_url_thumb'], UPLOAD_ABS_TEMP.$photo_values['photo_url_thumb']);
				}
				$_SESSION['hotel']['photos'] = $current_photos;
			}
			
			$template->head_control();
			display_form($_SESSION['hotel']);
			$template->foot_control();	
					
		}else{
			header("Location: hotels.php");
			exit;
		}

    break;
    case "step_2";
	
		$template->head_control();
		
		$_SESSION['hotel']['name'] = $_REQUEST['name']; 
		$_SESSION['hotel']['description'] = $_REQUEST['description'];
		$_SESSION['hotel']['hotel_rating'] = $_REQUEST['hotel_rating'];
		$_SESSION['hotel']['parent_id'] = $_REQUEST['parent_id'];		
		$_SESSION['hotel']['address'] = $_REQUEST['address'];
		$_SESSION['hotel']['longtitude'] = $_REQUEST['longtitude'];
		$_SESSION['hotel']['latitude'] = $_REQUEST['latitude'];
		$_SESSION['hotel']['show_google_map'] = $_REQUEST['show_google_map'];
		$_SESSION['hotel']['website_url'] = $_REQUEST['website_url'];
		$_SESSION['hotel']['notes'] = $_REQUEST['notes'];
		
		
		// populate tree values
		$tree = new tree();
		$params['tree_id'] = $_SESSION['hotel']['parent_id'];
		$values = $tree->get_branch($params);
		$values = $values[0];
		if($_SESSION['hotel']['type'] == "edit"){
			$_SESSION['branch']['tree_id'] = $_SESSION['hotel']['tree_id'];
		}
		$_SESSION['branch']['type_id'] = '3';
		$_SESSION['branch']['parent_id'] = $values['tree_id'];
		$_SESSION['branch']['lineage'] = $values['lineage'].",".$values['tree_id'];
		
		
        $hotels = new hotels();
        $errors_found = $hotels->validate_details_form($_SESSION['hotel']);
		
        if($errors_found) {
			$my_message = array();
			$my_message['type'] = 'neg';
			$my_message['message'] = "There were problems with the form";
			$my_message['list'] = $errors_found;
			$GLOBALS['note'] = $my_message;
            display_form($_SESSION['hotel']);
        }else{
            display_photos_form();
        }
		$template->foot_control();
    break;
	
	
   case "upload_photo";
	

	
		if(!isset($_SESSION['hotel'])){
			// redirect browser first step if there is no advert session data
			header("Location: hotels.php");
			exit;
		}
	
		// Check to see if the form was submitted
		// If yes then set the values
		if(isset($_POST['form_submitted'])){

        for($i=0; $i<10; $i++){
		
            if($_FILES['photo_'.$i]['size']){
				
                $my_upload = new uploader();
                $upload = $my_upload->upload_file('photo_'.$i, MAX_FILE_SIZE);
				
                if($upload['error']){
                    
					$my_array_errors = array($upload['error']);
					
                }else{
					
					// create a unique name
                    $unique_file_name_img = unique_id();
					// Create a full size image from uploaded image
                    $my_upload->optimise_photo_image(FULL_HEIGHT, FULL_WIDTH, $upload['source'], UPLOAD_ABS_TEMP, $unique_file_name_img, $upload['ext']);
					
					// create a unique name
					$unique_file_name_img_thumb = unique_id(); 
					// Create a smaller size image from uploaded image
                    $my_upload->optimise_photo_image(THUMB_HEIGHT, THUMB_WIDTH, $upload['source'], UPLOAD_ABS_TEMP, $unique_file_name_img_thumb, $upload['ext']);
					
					// add the photo urls to the advert session
					$_SESSION['hotel']['photos'][$i]['photo_url_full'] = $unique_file_name_img.".jpg"; 
					$_SESSION['hotel']['photos'][$i]['photo_url_thumb'] = $unique_file_name_img_thumb.".jpg"; 
	
					
                }
            }
        }
		
			// redirect browser to search results page
			// avoid form being resubmitted repeat when refreshing browser
			header("Location: hotels.php?action=upload_photo");
			exit;
		
		
		}else{
		
			$template->head_control();
	
			display_photos_form();
	
			$template->foot_control();	
		
		}
		
		
		
    break;
    case "delete_photo";
	
		if(!isset($_SESSION['hotel'])){
			// redirect browser first step if there is no advert session data
			header("Location: /hotels.php");
			exit;
		}
		
		$delete_no = $_REQUEST['photo_no']; 

		unset($_SESSION['hotel']['photos'][$delete_no]);
		
		$new_advert_photos_array = array();
		
		foreach($_SESSION['hotel']['photos'] as $values){
			$new_advert_photos_array[] = $values;
		}
		
		// Delete session values
		unset($_SESSION['hotel']['photos']);
		// Reasign values
		$_SESSION['hotel']['photos'] = $new_advert_photos_array;
		
		$template->head_control();

		display_photos_form();

		$template->foot_control();
		
    break;
	case "make_main":
	
		if(!isset($_SESSION['hotel'])){
			// redirect browser first step if there is no advert session data
			header("Location: /ads/");
			exit;
		}


		$c_photos = $_SESSION['hotel']['photos'];
		$n_photos = array();
		$main_photo_no = (int)$_GET['photo_no'];
		
		$n_photos[] = $c_photos[$main_photo_no];
		
		$i = 0;
		

		foreach($c_photos as $c_photo){
			
			if($i !== $main_photo_no){
				$n_photos[] = $c_photo;
			}
			
			$i++;
		}
		
		$_SESSION['hotel']['photos'] = $n_photos;
		
		$template->head_control();

		display_photos_form();

		$template->foot_control();

	
	break;
	
	
	
	
    case "step_3";
		
		$template->head_control();
        preview_advert($_SESSION['hotel']);
		$template->foot_control();
		
    break;

    
    case "step_4"; // COMIT DETAILS
	
		$tree = new tree();
		$hotels = new hotels();
        $my_upload = new uploader();
        $photos = new photos();
	
       
		// get main_photo value
		$_SESSION['hotel']['main_photo'] = $_SESSION['hotel']['photos'][0]['photo_url_thumb'];
		
		if(!$_SESSION['hotel']['photos'][0]['photo_url_thumb']) $_SESSION['hotel']['main_photo'] = NULL;
		
		// Insert details
		if($_SESSION['hotel']['type'] == "new"){
			//Get lineage from resort id
			$params['tree_id'] = $_SESSION['hotel']['parent_id'];			
			$parent_branch = $tree->get_branch($params);
			$_SESSION['hotel']['lineage_base'] = $parent_branch[0]['lineage'];
			$_SESSION['hotel']['type_id'] = '3';
			// Insert values into tree table
			$tree_id = $tree->insert_branch($_SESSION['hotel'],3);
		}
		
		// update details
		if($_SESSION['hotel']['type'] == "edit"){
			
			//Get lineage from resort id
			$params['tree_id'] = $_SESSION['hotel']['parent_id'];
			$parent_branch = $tree->get_branch($params);
			$_SESSION['hotel']['lineage_base'] = $parent_branch[0]['lineage'];

			$tree_id = $_SESSION['hotel']['tree_id'];
			// Update hotel details
			$tree->update_branch($_SESSION['hotel'],3);
                
			if(!empty($_SESSION['hotel']['photos'])){
				// Delete photo files
				$all_exsiting_photos = $photos->get_photos($tree_id);
				if(!empty($all_exsiting_photos)){
					foreach($all_exsiting_photos as $exsiting_photo){
						@unlink(UPLOAD_ABS_PERM.$exsiting_photo['photo_url_thumb']);
						@unlink(UPLOAD_ABS_PERM.$exsiting_photo['photo_url_full']);
					}		
					// Delete previous photos entries 
					$photos->delete_photos($_SESSION['hotel']['tree_id']);
				}
			}
		}
		
		
		$photos->upload_form_photo($_SESSION['hotel']['photos'], $tree_id);

		// clear any old advert session
		unset($_SESSION['hotel']);  
		header("Location: hotels.php?action=uploaded&tree_id=".$tree_id);
		
    break;  
	
	case "uploaded";
		$my_message = array();
		$my_message['type'] = 'pos';
		$my_message['message'] = "Hotel succesfully updated";
		$GLOBALS['note'] = $my_message;
    
		$template->head_control();
        
		if(isset($_GET['tree_id'])){
			$tree = new tree();
			$params['tree_id'] = $_GET['tree_id'];
			$hotel_values = $tree->get_branch($params,3);
			view_details($hotel_values[0]);
		}
		
		$template->foot_control();
    break; 
	 
    case "photo_delete";
		$template->head_control();
        $destination = new destination();
        $destination->delete_photo($_REQUEST['tree_id'],$_REQUEST['photo_id']);
        display_photos_form($_REQUEST['tree_id'],'');
		$template->foot_control();
    break;  
	
	
	case "view";
		$template->head_control();
        
		if(isset($_GET['tree_id'])){
			$tree = new tree();
			$params['tree_id'] = $_GET['tree_id'];
			$hotel_values = $tree->get_branch($params,3);
			view_details($hotel_values[0]);
		}
		
		$template->foot_control();
	break;

	case "confirm_delete";
		$template->head_control();
		view_confirm_delete();
		$template->foot_control();

	break;
    
	case "delete_move";
	
		$hotels = new hotels();
		$photos = new photos();
		$tree = new tree();
		$reviews = new reviews();
		$hotel_instances_mcs = new hotel_instances_mcs();
		$hotel_instances_you = new hotel_instances_you();
		
		
		
		
		// Move records
		
		$move_to_tree_id = $_REQUEST['move_to_tree_id'];
		$delete_tree_id = $_REQUEST['delete_tree_id'];
		
		if(!empty($move_to_tree_id)){
		
			// Move Description
			if($_REQUEST['copy_description']=='true'){
				//get description
				$params['tree_id'] = $delete_tree_id;
				$hotel_values = $hotels->get_hotel($params);
				unset($params);
				
				//update description only
				$params['tree_id'] = $move_to_tree_id;
				$params['description'] = $hotel_values['description'];
				
				
				$hotels->update_hotel_description_only($params);
				unset($params);
			}
			
			// Move Photos
			if($_REQUEST['copy_photos']=='true'){
				// delete existing photos
				$photos->delete_photos($move_to_tree_id);
				//update photos
				$params['move_to_tree_id'] = $move_to_tree_id;
				$params['delete_tree_id'] = $delete_tree_id;
				$photos->move_update_photo($params);
				unset($params);
			}
			
			// Move Reviews
			if($_REQUEST['copy_reviews']=='true'){
				$params['move_to_tree_id'] = $move_to_tree_id;
				$params['delete_tree_id'] = $delete_tree_id;
				$reviews->move_update_review($params);
				unset($params);
			}
			
			// Move Micros Instances
			if($_REQUEST['copy_mcs_instances']=='true'){
				$params['move_to_tree_id'] = $move_to_tree_id;
				$params['delete_tree_id'] = $delete_tree_id;
				$hotel_instances_mcs->move_update_mcs_instances($params);
				unset($params);
			}
		
		}
		
		// Delete records
		
		// Delete Hotel
		$hotels->delete_hotel($delete_tree_id);
		
		// Delete branch
		$params['tree_id'] = $delete_tree_id;
		$tree->delete_branch($params);
		unset($params);
		
		// Delete photos
		$photos->delete_photos($delete_tree_id);
		
		// Delete reviews
		$reviews->delete_reviews($delete_tree_id);
		
		// Delete micros instances
		$hotel_instances_mcs->delete_instances_by_tree($delete_tree_id);
		
		// Delete youtravel instances
		$hotel_instances_you->delete_instances_by_tree($delete_tree_id);
		
		$my_message = array();
		$my_message['type'] = 'pos';
		$my_message['message'] = "The hotel has been removed from the system.";
		$GLOBALS['note'] = $my_message;

		$template->head_control();
		display_home();
		$template->foot_control();
		
	break;
}

    


?>